"""
输入框组件示例
"""
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, 
                            QHBoxLayout, QLabel, QSpacerItem, QSizePolicy, QFrame)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont

from component.input import Input
from component.button import Button
from component.theme import Theme

class MainWindow(QMainWindow):
    """输入框组件示例程序的主窗口"""
    
    def __init__(self):
        super().__init__()
        
        # 设置窗口属性
        self.setWindowTitle("输入框组件示例")
        self.resize(800, 600)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
        main_layout.setContentsMargins(40, 40, 40, 40)
        main_layout.setSpacing(20)
        
        # 标题
        title = QLabel("输入框组件示例")
        title.setFont(QFont("Arial", 24, QFont.Bold))
        main_layout.addWidget(title)
        
        # 描述
        description = QLabel("输入框组件提供了美观、功能丰富的文本输入界面，支持标签、占位文本、验证、清除按钮等功能。")
        description.setWordWrap(True)
        main_layout.addWidget(description)
        
        # 创建示例容器
        examples_container = QWidget()
        examples_layout = QVBoxLayout(examples_container)
        examples_layout.setContentsMargins(0, 20, 0, 0)
        examples_layout.setSpacing(30)
        
        # 基本输入框
        self._add_example_section(
            examples_layout,
            "基本输入框",
            "最基本的输入框组件，带有标签和占位文本。",
            [
                Input(label="用户名", placeholder="请输入用户名")
            ]
        )
        
        # 密码输入框
        password_input = Input(label="密码", placeholder="请输入密码", password_mode=True)
        self._add_example_section(
            examples_layout,
            "密码输入框",
            "用于输入密码的输入框，文本会被掩码显示。",
            [password_input]
        )
        
        # 禁用和只读输入框
        disabled_input = Input(label="禁用状态", placeholder="此输入框已禁用", disabled=True)
        readonly_input = Input(label="只读状态", placeholder="此输入框为只读")
        readonly_input.setText("只读内容")
        readonly_input.setReadOnly(True)
        
        self._add_example_section(
            examples_layout,
            "禁用和只读输入框",
            "展示输入框的禁用和只读状态。",
            [disabled_input, readonly_input]
        )
        
        # 带验证的输入框
        email_input = Input(label="邮箱", placeholder="请输入有效的邮箱地址")
        email_input.textChanged.connect(self._validate_email)
        
        self.email_validation_label = QLabel()
        self.email_validation_label.setStyleSheet(f"color: {Theme.DANGER};")
        
        email_container = QWidget()
        email_layout = QVBoxLayout(email_container)
        email_layout.setContentsMargins(0, 0, 0, 0)
        email_layout.setSpacing(5)
        email_layout.addWidget(email_input)
        email_layout.addWidget(self.email_validation_label)
        
        self._add_example_section(
            examples_layout,
            "带验证的输入框",
            "输入框可以与验证逻辑结合，提供实时反馈。",
            [email_container]
        )
        
        # 带按钮的输入框
        search_input = Input(label="搜索", placeholder="请输入搜索关键词")
        search_button = Button("搜索", button_type="primary")
        
        search_container = QWidget()
        search_layout = QHBoxLayout(search_container)
        search_layout.setContentsMargins(0, 0, 0, 0)
        search_layout.setSpacing(10)
        search_layout.addWidget(search_input)
        search_layout.addWidget(search_button)
        
        self._add_example_section(
            examples_layout,
            "带按钮的输入框",
            "输入框可以与按钮组合使用，例如搜索框。",
            [search_container]
        )
        
        # 添加示例容器到主布局
        main_layout.addWidget(examples_container)
        main_layout.addStretch()
    
    def _add_example_section(self, parent_layout, title, description, widgets):
        """添加示例部分"""
        section = QWidget()
        section_layout = QVBoxLayout(section)
        section_layout.setContentsMargins(0, 0, 0, 0)
        section_layout.setSpacing(10)
        
        # 标题
        title_label = QLabel(title)
        title_label.setFont(QFont("Arial", 16, QFont.Bold))
        section_layout.addWidget(title_label)
        
        # 描述
        desc_label = QLabel(description)
        desc_label.setWordWrap(True)
        section_layout.addWidget(desc_label)
        
        # 示例容器
        example_container = QFrame()
        example_container.setFrameShape(QFrame.StyledPanel)
        example_container.setStyleSheet(f"""
            QFrame {{
                background-color: {Theme.BG_COLOR};
                border: 1px solid {Theme.BORDER_LIGHT};
                border-radius: {Theme.BORDER_RADIUS_BASE}px;
            }}
        """)
        
        example_layout = QVBoxLayout(example_container)
        example_layout.setContentsMargins(20, 20, 20, 20)
        example_layout.setSpacing(15)
        
        # 添加组件
        for widget in widgets:
            example_layout.addWidget(widget)
        
        section_layout.addWidget(example_container)
        parent_layout.addWidget(section)
    
    def _validate_email(self, text):
        """验证邮箱格式"""
        import re
        
        if not text:
            self.email_validation_label.setText("")
            return
        
        # 简单的邮箱验证正则表达式
        email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
        
        if re.match(email_pattern, text):
            self.email_validation_label.setText("")
        else:
            self.email_validation_label.setText("请输入有效的邮箱地址")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    # 设置应用样式
    app.setStyleSheet(Theme().get_style_sheet())
    
    window = MainWindow()
    window.show()
    
    sys.exit(app.exec_()) 